package HousingService.task2.serialization.parser;

import HousingService.task2.serialization.parser.AbstractParser;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Transforms records from file into List<String[]>,
 * where each List[i] is String Array of objects' fields values
 *
 * Created by IntelliJ IDEA.
 * User: Linderhof
 * Date: 02.12.2010
 * Time: 22:08:19
 */
public class JSNParser extends AbstractParser {
    /**
     * While not EOF, readLine()
     *  if (line == objectOp)
     *      parseObject() //here lines are also read
     *
     * @param file
     * @return  List of ObjectFields for all objects, stored in the file 
     */
    @Override
    public List<String[]> parse(File file) {
        List<String[]> strings =
                new ArrayList<String[]>();

        Pattern field =
                Pattern.compile("\\s*\"(\\w+)\":\\s*\"(\\w+)\",?");
        Pattern objectEnd =
                Pattern.compile("\\s*(\\}\\})\\s*");
        /*Pattern memberObjectOp =
                Pattern.compile("\\s*\"(\\w+)\":\\s*\\{\\s*");
        Pattern memberObjectEnd =
                Pattern.compile("\\s*\\}\\s*");
        Pattern objectOp =
                Pattern.compile("\\s*\\{\"([a-zA-Z0-9]+.*[a-zA-Z0-9]+)\":\\s*\\{\\s*");*/

        Matcher mObjectOp;
        Matcher mObjectEnd;
        Matcher mField;


        FileReader reader = null;
        BufferedReader in = null;

        try {
            reader = new FileReader(file);
            in = new BufferedReader(reader);

            String line;
            //Stores field values of the Object, stored in the file
            //values are separated by ";"
            StringBuilder buff = new StringBuilder(";");

            while((line = in.readLine()) != null) {
                mField = field.matcher(line);
                if(mField.matches()) {
                    buff.append(mField.group(2));
                    buff.append(";");
                }

                //if ObjectEnd is reached - add(String[]);
                mObjectEnd = objectEnd.matcher(line);
                if(mObjectEnd.matches()) {
                    buff.deleteCharAt(0);               //removes empty space;
                    String str = buff.toString();
                    strings.add(str.split(";"));
                    buff.delete(0, buff.length()-1);    //resets buff
                }
            }//while
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    //ignore
                }
            }
        }//finally
        
        return strings;
    }
}
